//
//  BillingDetailVC.m
//  MyNewProject
//
//  Created by sun on 2019/2/12.
//  Copyright © 2019 sun. All rights reserved.
//
#define kTabDefautHeight 47.0
#import "BillingDetailVC.h"

#import "FLDataModels.h"
#import "JFDataModels.h"

@interface BillingDetailVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView * myTalbleView;
@property(nonatomic,strong)NSMutableArray * myData;
@property(nonatomic,assign)NSInteger page;

@end

@implementation BillingDetailVC


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.gk_navigationBar.hidden = YES;

    self.myData = [NSMutableArray array];

    self.page = 1;
    [self.view addSubview:self.myTalbleView];
   
    [self.myTalbleView.mj_header beginRefreshing];

}
-(void)loadData
{
    NSString * url = nil;
    NSDictionary * dic =  dic = @{@"uid":self.uid,@"loginKey":self.loginKey,@"page":[NSString stringWithFormat:@"%ld",(long)self.page]};;
    if (self.myBillVC == FANLIVC) {
        url = @"my/getPayLog";

    }
    else
    {
        url = @"Jifen/getJifenLog";
    }
    SKWeakSelf;
    
    [YQNetworking postWithUrl:url params:dic reloginRefreshBlock:^(id response) {
        
    } successBlock:^(id response) {
        
        [weakSelf.myTalbleView.mj_header endRefreshing];
        [weakSelf.myTalbleView.mj_footer endRefreshing];
        
        if (weakSelf.myBillVC == FANLIVC) {
            
            FLBaseClass * baseClass=[FLBaseClass modelObjectWithDictionary:response];
            if ([baseClass.status isEqualToString:@"ok"]||[baseClass.status isEqualToString:@"empty"]) {
                
                if (weakSelf.page == 1) {
                    [weakSelf.myData removeAllObjects];
                    
                }
                if (baseClass.data.count >0) {
                    [self.myData addObjectsFromArray:baseClass.data];
                    
                }
                else
                {
                    [weakSelf.myTalbleView.mj_footer endRefreshingWithNoMoreData];
                    [weakSelf.view makeToast:@"没有更多数据" duration:1 position: CSToastPositionCenter];
                }
                
                [weakSelf.myTalbleView reloadData];
            }
            else
            {
                
//                [weakSelf.view makeToast:SafeString(@"加载失败") duration:1 position: CSToastPositionCenter];

            }
            
        }
        else
        {
            
            JFBaseClass * baseClass=[JFBaseClass modelObjectWithDictionary:response];
            if ([baseClass.status isEqualToString:@"ok"]) {
                
                if (weakSelf.page == 1) {
                    [weakSelf.myData removeAllObjects];
                    
                }
                if (baseClass.data.count >0) {
                    [self.myData addObjectsFromArray:baseClass.data];
                    
                }
                else
                {
                    [weakSelf.myTalbleView.mj_footer endRefreshingWithNoMoreData];
                    [weakSelf.view makeToast:@"没有更多数据" duration:1 position: CSToastPositionCenter];
                }
                [weakSelf.myTalbleView reloadData];
                
                
            }
            else
            {
                
//                [weakSelf.view makeToast:SafeString(@"加载失败") duration:1 position: CSToastPositionCenter];
                
            }
            
        }
        
        
    } failBlock:^(NSError *error) {
        [weakSelf.myTalbleView.mj_header endRefreshing];
        [weakSelf.myTalbleView.mj_footer endRefreshing];
        
        [weakSelf.view makeToast:SafeString(@"加载失败") duration:1 position: CSToastPositionCenter];
        
    }];
//    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:dic progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
//
//    } successBlock:^(id response) {
//
//        [weakSelf.myTalbleView.mj_header endRefreshing];
//        [weakSelf.myTalbleView.mj_footer endRefreshing];
//
//        if (weakSelf.myBillVC == FANLIVC) {
//
//            FLBaseClass * baseClass=[FLBaseClass modelObjectWithDictionary:response];
//            if ([baseClass.status isEqualToString:@"ok"]) {
//
//                if (weakSelf.page == 1) {
//                    [weakSelf.myData removeAllObjects];
//
//                }
//                if (baseClass.data.count >0) {
//                    [self.myData addObjectsFromArray:baseClass.data];
//
//                }
//                else
//                {
//                    [weakSelf.myTalbleView.mj_footer endRefreshingWithNoMoreData];
//                    [weakSelf.view makeToast:@"没有更多数据" duration:1 position: CSToastPositionCenter];
//                }
//
//                [weakSelf.myTalbleView reloadData];
//            }
//            else
//            {
//
//                [weakSelf.view makeToast:SafeString(@"加载失败") duration:1 position: CSToastPositionCenter];
//
//            }
//
//        }
//        else
//        {
//
//            JFBaseClass * baseClass=[JFBaseClass modelObjectWithDictionary:response];
//            if ([baseClass.status isEqualToString:@"ok"]) {
//
//                if (weakSelf.page == 1) {
//                    [weakSelf.myData removeAllObjects];
//
//                }
//                if (baseClass.data.count >0) {
//                    [self.myData addObjectsFromArray:baseClass.data];
//
//                }
//                else
//                {
//                    [weakSelf.myTalbleView.mj_footer endRefreshingWithNoMoreData];
//                    [weakSelf.view makeToast:@"没有更多数据" duration:1 position: CSToastPositionCenter];
//                }
//                [weakSelf.myTalbleView reloadData];
//
//
//            }
//            else
//            {
//
//                [weakSelf.view makeToast:SafeString(@"加载失败") duration:1 position: CSToastPositionCenter];
//
//            }
//
//        }
//
//
//    } failBlock:^(NSError *error) {
//        [weakSelf.myTalbleView.mj_header endRefreshing];
//        [weakSelf.myTalbleView.mj_footer endRefreshing];
//
//        [weakSelf.view makeToast:SafeString(@"加载失败") duration:1 position: CSToastPositionCenter];
//
//    }];
}
-(UITableView*)myTalbleView
{

    if (_myTalbleView ==nil) {
        _myTalbleView =[[UITableView alloc] initWithFrame:CGRectMake(0,0 ,SCREEN_WIDTH , SCREENH_HEIGHT-SKTopBar_Height-kTabDefautHeight) style:UITableViewStyleGrouped];
        _myTalbleView.dataSource = self;
        _myTalbleView.delegate = self;
        _myTalbleView.estimatedRowHeight = 0;
        _myTalbleView.estimatedSectionFooterHeight = 0;
        _myTalbleView.estimatedSectionHeaderHeight = 0;
        _myTalbleView.separatorStyle = UITableViewCellSeparatorStyleNone;
        if (@available(iOS 11.0, *)) {
            
            _myTalbleView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        } else {
            
            self.automaticallyAdjustsScrollViewInsets = NO;
            
        }
        
        SKWeakSelf;
        _myTalbleView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            weakSelf.page  = 1;
            if (weakSelf.myData.count>0) {
                [weakSelf.myTalbleView.mj_footer resetNoMoreData];
                
            }
            
            [weakSelf loadData];
        }];
        
        _myTalbleView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            weakSelf.page ++;
            
            [weakSelf loadData];
            
            
        }];
        _myTalbleView.mj_footer.automaticallyHidden = YES;
        
    }
    return _myTalbleView;
}

#pragma mark -  UITableView 代理相关

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger numberOfRows = _myData.count;
   
    return numberOfRows;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!cell) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    }
    
    for (id view in [cell.contentView subviews])
    {
        
        [view removeFromSuperview];
        
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UILabel * titleLab = [[UILabel alloc] initWithFrame:CGRectMake(20, 15, 200, 25)];
    titleLab.textColor = SKRGBColor(52, 52, 52);
    titleLab.font = [UIFont systemFontOfSize:18.0f];
    titleLab.textAlignment = NSTextAlignmentLeft;
    [cell.contentView addSubview:titleLab];
    
    
    UILabel * timeLab = [[UILabel alloc] initWithFrame:CGRectMake(20, titleLab.bottom, 200, 25)];
    timeLab.textColor = SKRGBColor(135, 135, 135);
    timeLab.font = [UIFont systemFontOfSize:12.0f];
    timeLab.textAlignment = NSTextAlignmentLeft;
    [cell.contentView addSubview:timeLab];
    
    
    UILabel * priceLab = [[UILabel alloc] initWithFrame:CGRectMake(timeLab.right +15 , titleLab.bottom, SCREEN_WIDTH - timeLab.right -15 -20, 25)];

    priceLab.font = [UIFont systemFontOfSize:18.0f];
    priceLab.textAlignment = NSTextAlignmentRight;
    [cell.contentView addSubview:priceLab];
    
    
    UIImageView * imageView =[[UIImageView alloc] initWithFrame:CGRectMake(0, priceLab.bottom +15-.5, SCREEN_WIDTH, 0.5)];
    imageView.backgroundColor =SKRGBColor(237, 237, 237);

    if (_myBillVC == FANLIVC) {

        if (_myData.count>0) {
            [cell.contentView addSubview:imageView];

            FLData * flData = [_myData objectAtIndex:indexPath.row];
            timeLab.text = flData.time;

            //        1充值2提现3竞拍支出4竞拍收入5分享收入7积分兑换10竞拍失效扣除保证金
            switch ([flData.type intValue]) {
                case 1:
                {
                    titleLab.text = @"充值";
                    priceLab.textColor = SKColorWithHex(0x20A40E);

                    priceLab.text = [NSString stringWithFormat:@"+%.2f",[flData.money floatValue]];

                }
                    break;
                case 2:
                {
                    titleLab.text = @"提现";
                    priceLab.textColor = SKColorWithHex(0xFF2424);
                    priceLab.text = [NSString stringWithFormat:@"-%.2f",[flData.money floatValue]];

                }
                    break;

                case 3:
                {
                    titleLab.text = @"竞拍支出";
                    priceLab.textColor = SKColorWithHex(0xFF2424);
                    priceLab.text = [NSString stringWithFormat:@"-%.2f",[flData.money floatValue]];


                }
                    break;

                case 4:
                {
                    titleLab.text = @"竞拍收入";
                    priceLab.textColor = SKColorWithHex(0x20A40E);
                    priceLab.text = [NSString stringWithFormat:@"+%.2f",[flData.money floatValue]];

                }
                    break;
                case 5:
                {
                    titleLab.text = @"分享收入";
                    priceLab.textColor = SKColorWithHex(0x20A40E);
                    priceLab.text = [NSString stringWithFormat:@"+%.2f",[flData.money floatValue]];

                }
                    break;
                case 7:
                {
                    titleLab.text = @"积分兑换";
                    priceLab.textColor = SKColorWithHex(0xFF2424);
                    priceLab.text = [NSString stringWithFormat:@"-%.2f",[flData.money floatValue]];

                }
                    break;
                case 8:
                {
                    titleLab.text = @"冻结金额";
                    priceLab.textColor = SKColorWithHex(0xFF2424);
                    priceLab.text = [NSString stringWithFormat:@"-%.2f",[flData.money floatValue]];

                }
                    break;
                case 9:
                {
                    titleLab.text = @"释放金额";
                    priceLab.textColor = SKColorWithHex(0x20A40E);
                    priceLab.text = [NSString stringWithFormat:@"+%.2f",[flData.money floatValue]];

                }
                    break;
                case 10:
                {
                    titleLab.text = @"竞拍失效扣除保证金";
                    priceLab.textColor = SKColorWithHex(0xFF2424);
                    priceLab.text = [NSString stringWithFormat:@"-%.2f",[flData.money floatValue]];

                }
                    break;


                default:
                    break;
            }

        }



    }
    else
    {
        if (_myData.count>0) {
            [cell.contentView addSubview:imageView];

            JFData * flData = [_myData objectAtIndex:indexPath.row];
            //       
            timeLab.text = flData.time;

            switch ([flData.type intValue]) {
                case 6:
                {
                    titleLab.text = @"积分收入";
                    priceLab.textColor = SKColorWithHex(0x20A40E);

                    priceLab.text = [NSString stringWithFormat:@"+%.2f",[flData.jifen floatValue]];

                }
                    break;
                case 7:
                {
                    titleLab.text = @"积分支出";
                    priceLab.textColor = SKColorWithHex(0xFF2424);
                    priceLab.text = [NSString stringWithFormat:@"-%.2f",[flData.jifen floatValue]];

                }
                    break;




                default:
                    break;
            }

        }



    }
    
    
    return cell;
    
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 80;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    
    return nil;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return CGFLOAT_MIN;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
